<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<% 
 String operation = request.getParameter("op");
 String id = request.getParameter("id");
 %>
<!DOCTYPE html>
<html lang="en">
	<head>
		<%@ include file="/common/common-css.jsp" %>
		
		<!-- loading -->
		<link rel="stylesheet" href="/loan_manager/css/common-loading.css">
			
		<style>
			.table > th {
			  vertical-align: center;
			  background-color: grey;
			}
		</style>		
	</head>
	<body class="no-skin">
		
		<!-- loading -->
		<%@ include file="/common/common-loading.jsp"%>
		
		<div class="page-content">
			<div id="page-header" class="page-header">
				<h1>
					新增渠道
				</h1>
			</div>
			<div class="page-content-area">
				<div class="row">
					<div class="col-xs-12 col-sm-12 widget-container-col">
						<div class="widget-box widget-color-blue">
							<!-- #section:custom/widget-box.options.collapsed -->
							<div class="widget-header widget-header-small">
								<h6 class="widget-title lighter">
									<i class="ace-icon glyphicon glyphicon-th-list"></i>编辑
								</h6>
	                            <div class="widget-toolbar no-border">
									<a href="#" data-action="collapse">
										<i class="ace-icon fa fa-chevron-up"></i>
									</a>
								</div>
							</div>
							
							<div class="widget-body">
								<div class="widget-main" >
									<form class="form-horizontal" id="channel_form" method="post" enctype="multipart/form-data" >
										<input type="hidden" name="id"  id="id" placeholder="" class="col-xs-12 col-sm-6">
										<table id="baseinfo" class="table table-bordered table-striped table-hover table-condensed table-responsive table">
											
											<tr>
												<th style="width:100px">渠道名称</th>
													<td colspan="7">
														<input type="text" name="channel_name"  id="channel_name" placeholder="" class="col-xs-12 col-sm-4" />
													</td>
											</tr>
											
											<tr>
												<th style="width:100px">渠道号</th>
													<td colspan="7">
														<input type="text" name="channel_code"  id="channel_code" placeholder="" class="col-xs-12 col-sm-4" />
													</td>
											</tr>
											
											<tr>
												<th style="width:100px">产品</th>
													<td colspan="7">
													<!-- 	 <select class="col-sm-6" name="prdid" id="prdid">
														  <option value="">-请选择-</option>
														  <option value="-1">-车主无忧-</option>
														  <option value="1005">-值得买-</option>
														  <option value="1007">-无忧查违章-</option>
														  <option value="1008">-违章查询-</option>
														  <option value="4000">-财迷之家-</option>
														</select> -->
														 <s:select id="prdid" 
																	name="prdid" value="%{paramsMap.code}"
																	list="%{paramsMap.productList}" listKey="code"
																	listValue="kindname" cssClass="col-sm-6"
																	headerKey="" headerValue="---请选择">
											 			</s:select>
											 			
											 			
													</td>
											</tr>
										</table>
									</form>
								</div><!-- /.widget-main -->
							</div><!-- /.widget-body -->
						</div><!-- /.widget-box -->
					</div><!-- /.col-sm-12 -->
				</div><!-- /.row -->
				<div class="clearfix form-actions">
					<div class="col-md-offset-3 col-md-9">
						<button class="btn  btn-inverse  no-border " onclick="doCancel()" id="delete"> 
							<i class="ace-icon fa fa-undo bigger-160"></i>
							取消
						</button>
		
						&nbsp; &nbsp; &nbsp;
						<button id="btn-save" name="btn-save" class="btn  btn-success  no-border" type="button" onclick="doSave()">
							<i class="ace-icon fa fa-floppy-o bigger-160"></i>
							保存
						</button>
					</div>
				</div>
				</div>
			</div>
		</div>
	</body>
</html>

<%@ include file="/common/common-js.jsp" %>
<!-- loading js -->
<script type="text/javascript" src="${ctx}/js/common-loading.js"></script>
<script type="text/javascript" src="${ctx}/treasure/common_js/digital_utils.js?v=1"></script>

<script>

	//操作，0表示新增，1表示更新
	var operation = getQueryString('op');
	var channel_name = getQueryString('channel_name');
	var channel_code =  getQueryString('channel_code');
	var prdid = getQueryString('prdid');
	
	//如果是更新，channelid
	var id = "<%=id%>";
		
	$(function(){
		$("#id").val(id);
		
		//标题
		if (operation == 0) {
			$("#page-header h1").html("新增渠道");
		} else {
			$("#page-header h1").html("更新渠道");
			$("#channel_name").val(channel_name);
			$("#channel_code").val(channel_code);
			$("#prdid").val(prdid);
		}
	});
	
	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	 	var r = window.location.search.substr(1).match(reg);
		if (r != null) return decodeURI(r[2]); return null;
		
	};
	
	function getChannelContent() {
		var requestUrl = ctx + "/channel/updateChannel.action?rd=" + new Date().getTime();
		var data = "id=" + id;
		$.ajax({
			type: "post",
			timeout: 30000,
			data:data,
			url: requestUrl,
			async: true,
			dataType: "json",
			beforeSend: function() {
				showLoading();
			},
			success: function(data) {
				initViewByData(data);
			},
			error: function(xhr, status, errorThrown) {
			},
			complete: function(xhr, status) {
				hideLoading();
			}
		});
	};
	
	/**
	 * 数据更新界面的方法
	 * @param {Object} data
	 */
	function initViewByData(data) {
		var channel = data.channel;
		$("#channel").val(channel);
		
		var channelname = data.channelname;
		$("#channelname").val(channelname);
	};
	
	function doCancel() {
		parent.parent['__extdialog_active'].dialog('close');
	};
	
	
	function doSave() {
		//数据校验
		var name = $("#channel_name").val();
		if (name == null || name.trim().length <= 0) {
			alert("请填写渠道名称");
			return;
		}
		var requestUrl;
		if (operation == 0) {
			requestUrl = ctx + "/channel/addNewChannel.action?rd=" + new Date().getTime();
		} else {
			requestUrl = ctx + "/channel/updateChannel.action?rd=" + new Date().getTime();
		}
		//请求地址
		var iconChooseForm = document.getElementById("channel_form");
		var formData = new FormData(iconChooseForm);
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			if(xhr.readyState==4) {
				if(xhr.status==200) {
					alert("数据保存成功");
					parent.parent['__extdialog_active'].dialog('close');
				    parent.main_frame.query_any();
				} else {
					alert("数据保存失败");
				}
				hideLoading();
			}
		};    
		xhr.open("POST" ,requestUrl , true);
		xhr.send(formData);
		showLoading();
	};
	

	  jQuery(function() {
	 		$("#prdid").combobox();
	});
	
</script>	
